home *** CD-ROM | disk | FTP | other *** search
- Path: mozo.cc.purdue.edu!not-for-mail
- From: emurac@cs.purdue.edu (Christopher Emura)
- Newsgroups: comp.lang.c
- Subject: Memory Allocation (calloc()) question.
- Date: 24 Feb 1996 03:18:21 GMT
- Organization: Purdue University
- Message-ID: <4gm01t$spp@mozo.cc.purdue.edu>
- NNTP-Posting-Host: lore.cs.purdue.edu
- X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
-
-
- Why doesn't this section of code work? The memory made available by
- calloc and pointed to by T is "saved" in A right? No segmentation
- faults, just incorrect values when inserting in the "double array"
- block. I.e. if the original array never needs to be doubled, this
- function works.
-
-
- int insert(elementType *A, elementType x,
- int *currentSize, int *size, float percent) {
-
- /* will double array size when percent capacity is reached */
-
- /* check size first */
- if ( *currentSize > (*size * percent) ) {
- /* need to double the array */
- int i;
- elementType *T;
-
- *size = (int) *size * 2;
- T = calloc(*size, sizeof(elementType));
- for(i=0; i < *currentSize; i=i+1) {
- T[i] = A[i];
- }
- fprintf(stderr,"Flag 1 trying to do %d at T[%d]\n",x,*currentSize);
- T[*currentSize] = x;
-
- *currentSize = *currentSize + 1;
- A = T;
- }
-
- else {
- /* simply insert the element */
- A[*currentSize] = x;
- *currentSize = *currentSize + 1;
- }
-
- return(0);
- }
-